package com.heima.article.listener;
// 爱情不是随便许诺,算了,不想再说了

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.article.service.ApArticleService;
import com.heima.common.constants.message.WmNewsUpDownConstants;
import com.heima.common.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;

import static com.heima.common.constants.message.WmNewsUpDownConstants.WM_NEWS_UP_OR_DOWN_TOPIC;

/**
 * 拦截器
 */
@Component
@Slf4j
public class WmNewsUpDownListener {

    @Autowired
    private ApArticleService apArticleService;


    @KafkaListener(topics = WM_NEWS_UP_OR_DOWN_TOPIC)
    public void receiveMsg(ConsumerRecord<String,String> record){
        // 判断是否为空
        Optional<ConsumerRecord<String, String>> optional = Optional.ofNullable(record);
        if (optional.isPresent()){
            // 如果不为空,获得对象
            String value = record.value();
            log.info("[article]接收到信息,value = {}" ,value);
            // 判断值是否为空
            if (StringUtils.isNotEmpty(value)) {
                Map<String, Long> map = JsonUtils.toMap(value, String.class, Long.class);
                // 调用 apArticleService 的上下架
                apArticleService.dowmOrUp(map);
            }
        }
    }
}
